﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Drawing.Printing;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using DrugsShopManagement.DBCLASS;

namespace DrugsShopManagement
{
    public partial class FrmDrugsSales : DevExpress.XtraEditors.XtraForm
    {
        public FrmDrugsSales(string user_no,string user_name)
        {
            InitializeComponent();
            u_no = user_no;
            u_name = user_name;
        }
        Operator op = new Operator();
        public string  u_no,u_name;

        private FrmDrugsSalesDetail frmDrugsSalesDetail;

        private void FrmDrugsSales_Load(object sender, EventArgs e)
        {
            //获取用户工号
            this.txtBoxKPYID.Text = u_no;

            this.WindowState = FormWindowState.Maximized;
            try
            {
                DataTable dt1 = new DataTable();
                string sql1 = "select drugsOutID 出库单号,drugsOutID 出库ID,drugsId 药品ID,drugsName 药品名称,drugsUnit 计量单位,drugsPY 拼音码,drugsSpecification 药品规格,drugsBatchNo 批号,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutIdnum 出库ID,providerName 开票员,drugsCount 库存数量,drugsInPrice 进货单价,drugsSalePrice 销售单价,drugsOutNo 出库数量,drugsOutMoney 出库金额,drugsOutDate 出库日期 from drugsOut order by drugsOutID";
                dt1 = op.getDataTable(sql1, "drugsOut"); 
                dataGridView1.DataSource = dt1;
                this.tbdrugsSalesMan.Text = u_name;
               
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                DataTable dt_1 = new DataTable();
                dt_1 = op.getDataTable("select * from drugsOut where drugsOutID='" + dataGridView1.CurrentRow.Cells[0].Value.ToString() + "'", "drugsOut");
                this.tbdrugsOutID.Text = dt_1.Rows[0]["drugsOutID"].ToString();
                this.tbdrugsID.Text = dt_1.Rows[0]["drugsId"].ToString();
                this.tbdrugsName.Text = dt_1.Rows[0]["drugsName"].ToString();
                this.tbdrugsPY.Text = dt_1.Rows[0]["drugsPY"].ToString();
                this.tbdrugsSpecification.Text = dt_1.Rows[0]["drugsSpecification"].ToString();
                this.tbdrugsUnit.Text = dt_1.Rows[0]["drugsUnit"].ToString();
                //this.textBox9.Text = dt_1.Rows[0]["drugsRegNum"].ToString();
                this.tbdrugsFactory.Text = dt_1.Rows[0]["drugsFactory"].ToString();
                this.tbdrugsSalePrice.Text = dt_1.Rows[0]["drugsSalePrice"].ToString();
                //this.tbdrugsSalesMoney.Text = dt_1.Rows[0]["drugsSalesMoney"].ToString();
                this.tbdrugsUsefulDate.Text = dt_1.Rows[0]["drugsUsefulDate"].ToString();
                this.tbdrugsOutNO.Text = dt_1.Rows[0]["drugsOutNo"].ToString();
                //生成销售单号
                GetDrugsSalesCode();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
     
        }

        private void textBox17_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = String.Format("select drugsOutID 出库ID,drugsId 药品ID,drugsName 药品名称,drugsUnit 计量单位,drugsPY 拼音码,drugsSpecification 药品规格,drugsOutNo 出库数量,drugsBatchNo 批号,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutIdnum 出库ID,providerName 开票员,drugsCount 库存数量,drugsInPrice 进货单价,drugsSalePrice 销售单价,drugsOutMoney 出库金额,drugsOutDate 出库日期 from drugsOut where drugsName like '%{0}%'or drugsPY like '%{1}%' order by drugsId", this.textBoxSearch.Text.Trim(), this.textBoxSearch.Text.ToUpper());
                dt = op.getDataTable(sql, "drugsOut");
                dataGridView1.DataSource = dt;
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (tbdrugsID.Text == "")
                {
                    MessageBox.Show("请选择需要销售的药品！");
                    return;
                }
                if (tbdrugsSalesNum.Text == "")
                {
                    MessageBox.Show("请选择需要销售的药品数量！");
                    return;
                }
                if (Convert.ToInt32(tbdrugsSalesNum.Text) > Convert.ToInt32(tbdrugsOutNO.Text))
                {
                    MessageBox.Show("销售的药品数量不能大于出库数量！");
                    return;
                }

                string sql_a = String.Format("insert into drugsUnSales(drugsId,drugsName,drugsPY,drugsSpecification,drugsUnit,drugsSalePrice,drugsSalesNum,drugsSalesTime,drugsFactory,drugsUsefulDate,drugsOutID,drugsSalesMan)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", tbdrugsID.Text, tbdrugsName.Text, tbdrugsPY.Text, tbdrugsSpecification.Text, tbdrugsUnit.Text, tbdrugsSalePrice.Text, tbdrugsSalesNum.Text, this.tbdrugsSalesTime.Value, tbdrugsFactory.Text, this.tbdrugsUsefulDate.Text,tbdrugsOutID.Text, this.tbdrugsSalesMan.Text);
                op.ExecuteSql(sql_a);
                string sql_1 = String.Format("insert into drugsSales(drugsId,drugsName,drugsPY,drugsSpecification,drugsUnit,drugsSalePrice,drugsSalesNum,drugsSalesTime,drugsFactory,drugsUsefulDate,drugsOutID,drugsSalesMan)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}')", tbdrugsID.Text, tbdrugsName.Text, tbdrugsPY.Text, tbdrugsSpecification.Text, tbdrugsUnit.Text, tbdrugsSalePrice.Text, tbdrugsSalesNum.Text, this.tbdrugsSalesTime.Value, tbdrugsFactory.Text, this.tbdrugsUsefulDate.Text,tbdrugsOutID.Text, this.tbdrugsSalesMan.Text);
                op.ExecuteSql(sql_1);

                int n = Convert.ToInt32(tbdrugsOutNO.Text);
                int m = Convert.ToInt32(tbdrugsSalesNum.Text);
                int a = n - m;
                DataTable dt3 = new DataTable();
                string sql = "update drugsOut set drugsOutNo=' " + a + "' where drugsOutID= '" + Convert.ToInt32(tbdrugsOutID.Text) + "'";
                dt3 = op.getDataTable(sql, "drugsOut");
                MessageBox.Show("药品【" + tbdrugsName.Text.ToString() + "】销售成功！", "温馨提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();

                
                DataTable dt1 = new DataTable();
                string sql1 = "select drugsOutID 出库单号,drugsOutID 出库ID,drugsId 药品ID,drugsName 药品名称,drugsUnit 计量单位,drugsPY 拼音码,drugsSpecification 药品规格,drugsCount 库存数量,drugsBatchNo 批号,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutIdnum 出库ID,providerName 开票员,drugsInPrice 进货单价,drugsSalePrice 销售单价,drugsOutNo 出库数量,drugsOutMoney 出库金额,drugsOutDate 出库日期 from drugsOut order by drugsOutID";
                dt1 = op.getDataTable(sql1, "drugsOut");
                dataGridView1.DataSource = dt1;
                this.tbdrugsID.Text = "";
                this.tbdrugsName.Text = "";
                this.tbdrugsFactory.Text = "";
                this.tbdrugsPY.Text = "";
                this.tbdrugsSalePrice.Text = "";
                this.tbdrugsSalesNum.Text = "";
                this.tbdrugsSpecification.Text = "";
                this.tbdrugsUnit.Text = "";
                this.tbdrugsUsefulDate.Text = "";
                this.txtBoxXSDH.Text = "";
                this.tbdrugsOutID.Text = "";
                this.tbdrugsOutNO.Text = "";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }



        private void tbdrugsID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                button1_Click_1(sender, e);
            }
        }

        private void btndetail_Click(object sender, EventArgs e)
        {
            frmDrugsSalesDetail = new FrmDrugsSalesDetail();
            frmDrugsSalesDetail.ShowDialog();
        }

        /// <summary>
        /// 生成销售单号
        /// </summary>
        private void GetDrugsSalesCode()
        {
            ////该语句获得201268格式的日期，弃用
            //string str = Convert.ToString(DateTime.Now.Date.Year) + Convert.ToString(DateTime.Now.Date.Month) + Convert.ToString(DateTime.Now.Date.Day);

            //设置dateTimePicker2可见，以便获得如20120608格式的日期
            this.dateTimePicker2.Visible = true;
            try
            {
                //生成销售单号，生成规则为：用户工号+日期+药品ID    如：10022012060910002
                this.txtBoxXSDH.Text = this.txtBoxKPYID.Text + this.dateTimePicker2.Text + tbdrugsID.Text;
            }
            catch (Exception)
            {
                //MessageBox.Show(ex.Message);
            }
            //设置dateTimePicker2不可见
            this.dateTimePicker2.Visible = false;
        }

        private void btnFresh_Click(object sender, EventArgs e)
        {
            try
            {
                
                DataTable dt1 = new DataTable();
                string sql1 = "select drugsOutID 出库ID,drugsId 药品ID,drugsName 药品名称,drugsUnit 计量单位,drugsPY 拼音码,drugsSpecification 药品规格,drugsBatchNo 批号,drugsFactory 生产厂家,drugsUsefulDate 有效期,drugsOutIdnum 出库ID,providerName 开票员,drugsCount 库存数量,drugsInPrice 进货单价,drugsSalePrice 销售单价,drugsOutNo 出库数量,drugsOutMoney 出库金额,drugsOutDate 出库日期 from drugsOut order by drugsOutID";
                dt1 = op.getDataTable(sql1, "drugsOut");
                dataGridView1.DataSource = dt1;
                
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
              try
            {
                DataTable dr = new DataTable();
                dr = op.getDataTable("select * from drugsOut where drugsId='" + tbdrugsID.Text + "'", "drugsOut");
                this.tbdrugsID.Text = dr.Rows[0]["drugsId"].ToString();
                this.tbdrugsName.Text = dr.Rows[0]["drugsName"].ToString();
                this.tbdrugsPY.Text = dr.Rows[0]["drugsPY"].ToString();
                this.tbdrugsSpecification.Text = dr.Rows[0]["drugsSpecification"].ToString();
                this.tbdrugsUnit.Text = dr.Rows[0]["drugsUnit"].ToString();
                //this.textBox9.Text = dt_1.Rows[0]["drugsRegNum"].ToString();
                this.tbdrugsFactory.Text = dr.Rows[0]["drugsFactory"].ToString();
                this.tbdrugsSalePrice.Text = dr.Rows[0]["drugsSalePrice"].ToString();
                //this.tbdrugsSalesMoney.Text = dt_1.Rows[0]["drugsSalesMoney"].ToString();
                this.tbdrugsUsefulDate.Text = dr.Rows[0]["drugsUsefulDate"].ToString();
                //生成销售单号
                GetDrugsSalesCode();
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); return; }
     
        }
        

    }
}